// pages/myorder/index.js
import loginFn from '../../utils/login.js';
import urls from '../../utils/urls.js';
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.initData();
		console.log('options:',options);
		wx.showLoading();
		if(options.status) {
			this.setData({
				activeIndex: options.status,
				borderPos: options.status*187+65
			})
		}
		this.getlist(() => {
			wx.hideLoading();
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// this.getlist()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		
		this.initData();
		this.getlist(() => {
 			wx.stopPullDownRefresh();
		})
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		this.getlist();
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

	initData(){
		this.setData({
			typeList: [
				{
					name: '待付款',
					value: 0,
					list: [],
					page: 1,
					total_number: 0
				},
				{
					name: '已付款',
					value: 1,
					list: [],
					page: 1,
					total_number: 0
				},
				{
					name: '已评价',
					value: 2,
					list: [],
					page: 1,
					total_number: 0
				},
				{
					name: '已退款',
					value: 9,
					list: [],
					page: 1,
					total_number: 0
				}
			],
			borderPos: 65,
			activeIndex: 0
		})
	},
	getlist: function(callback) { //订单列表
		let that = this;
		let index = this.data.activeIndex;
		let typeList = this.data.typeList;
		let order = typeList[index];
		let list = order.list;
		let page = order.page;
		if(page > 1 && list.length >= order.total_number) {
			callback && callback();
			return false;
		}

		let postData = {
			status: order.value,
			page: page
		}

		app.request({
			url: 'order/index',
			data: postData,
			sCallback:(res) => {
				if(res.code == 1) {
					list = list.concat(res.data.list);
					order.page ++;
					order.list = list;
					order.total_number = res.data.total_number;
					typeList[index] = order;
					that.setData({
						typeList: typeList
					})
				}
				callback && callback();
			}
		})
	},
	tabClick: function(e) { //切换tab
		var index = app.getBindParam(e, 'index');
		this.setData({
			activeIndex: index,
			borderPos: index*187+65
		})

		this.getlist();
	},
	goTopay: function(e) { //立即支付
		console.log('立即付款')
		console.log(e)
		wx.navigateTo({
			url: '/pages/order/index?orderId=' + e.detail.orderId + '&courseId=' + e.detail.courseId,
		})
	},
	commentFn: function(e) { //评论
		console.log('评论返回')
		console.log(e)
		wx.navigateTo({
			url: '/pages/scoreclass/index?list=' + JSON.stringify(e.detail.list),
		})
	}
})
